package game.web.dao.impl;

import game.web.dao.UserDAO;
import game.web.dao.dto.UserDTO;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import projectx.core.base.ProjectXSystemException;
import projectx.core.dao.AbstractDAO;
import projectx.core.dao.DataAccessException;
import projectx.core.dao.ExceptionHandler;
import projectx.core.dao.Parameter;
import projectx.core.dao.impl.PreparedStatementBuilder;

public class UserDAOImpl extends AbstractDAO implements UserDAO {

	public enum field {
		userid,username,firstpass,secpass,truename,contactno,level,charge,withdraw,bonus,returnmoney,winmoney,money,agentmoney,returnpos,profit,loginflag,logintimes,createtime,updatetime,createip,updateip;
	}

	public UserDAOImpl() throws ProjectXSystemException {
		super();
	}

	@Override
	public UserDTO execute(Parameter param) throws DataAccessException {
		System.out.println("UserDAOImpl  start param.id="+param.getObject("id"));
		UserDTO dto = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		int count = 0;
		String sql = null;
		try {
			_builder = (PreparedStatementBuilder) SqlLoader.getBuilders().get(
					"UserDAO_SELECT");
			List paramKeys = new ArrayList();
			sql = _builder.getSql(param, paramKeys);
			ps = _builder.createStatement(_conn, sql, false, false);
			_builder.setParameter(ps, param, paramKeys);
			
			debugSql();

			rs = ps.executeQuery();

			if (rs.next()) {
				String userid = getString(rs.getString(field.userid.ordinal()+1));
				String username = getString(rs.getString(field.username.ordinal()+1));
				String firstpass = getString(rs.getString(field.firstpass.ordinal()+1));
				String secpass = getString(rs.getString(field.secpass.ordinal()+1));
				String truename = getString(rs.getString(field.truename.ordinal()+1));
				String contactno = getString(rs.getString(field.contactno.ordinal()+1));
				double charge = rs.getDouble(field.charge.ordinal()+1);
				double withdraw = rs.getDouble(field.withdraw.ordinal()+1);
				double bonus = (rs.getDouble(field.bonus.ordinal()+1));
				double returnmoney = (rs.getDouble(field.returnmoney.ordinal()+1));
				double winmoney = (rs.getDouble(field.winmoney.ordinal()+1));
				double money = (rs.getDouble(field.money.ordinal()+1));
				int loginflag = (rs.getInt(field.loginflag.ordinal()+1));
				int logintimes = (rs.getInt(field.logintimes.ordinal()+1));
				int level = (rs.getInt(field.level.ordinal()+1));
				double agentmoney = (rs.getDouble(field.agentmoney.ordinal()+1));
				double returnpos = (rs.getDouble(field.returnpos.ordinal()+1));
				double profit = (rs.getDouble(field.profit.ordinal()+1));
				String createtime = (rs.getString(field.createtime.ordinal()+1));
				String updatetime = (rs.getString(field.updatetime.ordinal()+1));
				String createip = getString(rs.getString(field.createip.ordinal()+1));
				String updateip = getString(rs.getString(field.updateip.ordinal()+1));
				dto = new UserDTO();
				dto.setUserid(userid);
				dto.setUsername(username);
				dto.setFirstpass(firstpass);
				dto.setTruename(truename);
				dto.setContactno(contactno);
				dto.setLevel(level);
				dto.setSecpass(secpass);
				dto.setCharge(charge);
				dto.setWithdraw(withdraw);
				dto.setBonus(bonus);
				dto.setReturnmoney(returnmoney);
				dto.setWinmoney(winmoney);
				dto.setMoney(money);
				dto.setAgentmoney(agentmoney);
				dto.setReturnpos(returnpos);
				dto.setProfit(profit);
				dto.setLoginflag(loginflag);
				dto.setLogintimes(logintimes);
				dto.setCreatetime(createtime);
				dto.setUpdatetime(updatetime);
				dto.setCreateip(createip);
				dto.setUpdateip(updateip);
				count++;
			}

			if (count == 1 && rs.next()) {
				count++;
			}
		} catch (Exception e) {
			errorSql(e);
			_exHandler.handleQueryException(e, ExceptionHandler.UNKNOWN);
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (ps != null) {
					ps.close();
				}
			} catch (SQLException e) {
				logError(e);
			}
		}
		if (count > 1) {
			_exHandler.handleQueryException(null, ExceptionHandler.MULTIFOUND);
		}
		return dto;
	}

	@Override
	public boolean update(UserDTO dto) throws DataAccessException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		int count = 0;
		String sql = null;
		try {
			_builder = (PreparedStatementBuilder) SqlLoader.getBuilders().get(
					"UserDAO_UPDATE");
			List paramKeys = new ArrayList();
			Parameter param = new Parameter();
			param.setString("userid", dto.getUserid());
			param.setString("truename", dto.getTruename());
			param.setString("firstpass", dto.getFirstpass());
			param.setString("secpass", dto.getSecpass());
			param.setString("contactno", dto.getContactno());
			param.setString("updatetime", dto.getUpdatetime());
			param.setString("updateip", dto.getUpdateip());
			
			sql = _builder.getSql(param, paramKeys);
			ps = _builder.createStatement(_conn, sql, false, false);
			_builder.setParameter(ps, param, paramKeys);
			
			debugSql();

			return ps.execute();

		} catch (Exception e) {
			errorSql(e);
			_exHandler.handleQueryException(e, ExceptionHandler.UNKNOWN);
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (ps != null) {
					ps.close();
				}
			} catch (SQLException e) {
				logError(e);
			}
		}
		if (count > 1) {
			_exHandler.handleQueryException(null, ExceptionHandler.MULTIFOUND);
		}
		return false;
	}
}
